# 发版前API端点自检脚本 (PowerShell版本)
# 用途: 在构建产物中检查所有HTTP/HTTPS端点，确保没有写死的私网地址或旧域名
# 使用方法: 
#   1. 先执行构建: npm run build 或 vite build
#   2. 运行此脚本: PowerShell -ExecutionPolicy Bypass -File scripts/grep-api.ps1
#   3. 检查输出，确认只包含预期的端点

Write-Host "=== 发版前API端点自检 ===" -ForegroundColor Cyan
Write-Host "检查目录: dist/"

if (-not (Test-Path "dist")) {
    Write-Host "❌ 错误: dist 目录不存在，请先执行构建命令" -ForegroundColor Red
    exit 1
}

Write-Host ""
Write-Host "🔍 搜索所有 HTTP/HTTPS 端点..." -ForegroundColor Yellow

# 排除常见第三方CDN域名
$excludePatterns = @(
    "googleapis.com",
    "gstatic.com", 
    "unpkg.com",
    "jsdelivr.net",
    "cdnjs.com",
    "github.com",
    "element-plus.org",
    "w3.org",
    "mozilla.org",
    "microsoft.com",
    "apple.com"
)

Write-Host ""
Write-Host "📊 HTTP/HTTPS 端点统计:" -ForegroundColor Green
Write-Host "========================="

# 搜索所有HTTP/HTTPS端点
$urls = @()
Get-ChildItem -Path "dist" -Recurse -File | ForEach-Object {
    $content = Get-Content $_.FullName -Raw -ErrorAction SilentlyContinue
    if ($content) {
        $matches = [regex]::Matches($content, 'https?://[^"'')\\s]*')
        foreach ($match in $matches) {
            $urls += $match.Value
        }
    }
}

# 统计和显示结果
if ($urls.Count -gt 0) {
    $urlCounts = $urls | Group-Object | Sort-Object Count -Descending
    
    foreach ($urlGroup in $urlCounts) {
        $count = $urlGroup.Count
        $url = $urlGroup.Name
        
        # 检查是否为需要关注的URL
        if ($url -match "192\.168\.|^10\.|172\.") {
            Write-Host "⚠️  $count 次 - $url (私网地址)" -ForegroundColor Yellow
        }
        elseif ($url -match "localhost|127\.0\.0\.1") {
            Write-Host "⚠️  $count 次 - $url (本地地址)" -ForegroundColor Yellow
        }
        elseif ($url -match "47\.108\.58\.48") {
            Write-Host "⚠️  $count 次 - $url (旧服务器地址)" -ForegroundColor Yellow
        }
        else {
            # 检查是否为第三方CDN
            $isThirdParty = $false
            foreach ($pattern in $excludePatterns) {
                if ($url -match [regex]::Escape($pattern)) {
                    $isThirdParty = $true
                    break
                }
            }
            
            if ($isThirdParty) {
                Write-Host "✅ $count 次 - $url (第三方CDN)" -ForegroundColor Green
            }
            else {
                Write-Host "🔍 $count 次 - $url" -ForegroundColor White
            }
        }
    }
}
else {
    Write-Host "✅ 未发现任何HTTP/HTTPS端点" -ForegroundColor Green
}

Write-Host ""
Write-Host "========================="

# 特别检查预期的生产地址
$expectedProdUrl = "http://114.67.64.10"
$prodCount = ($urls | Where-Object { $_ -eq $expectedProdUrl }).Count
Write-Host "📋 预期生产地址统计:" -ForegroundColor Cyan
Write-Host "   $expectedProdUrl`: $prodCount 次"

Write-Host ""
Write-Host "🎯 检查完成!" -ForegroundColor Green
Write-Host ""
Write-Host "💡 验收建议:" -ForegroundColor Cyan
Write-Host "   1. 确认没有私网地址 (192.168.x.x, 10.x.x.x, 172.x.x.x)"
Write-Host "   2. 确认没有旧服务器地址 (47.108.58.48)"
Write-Host "   3. 确认生产地址 ($expectedProdUrl) 出现次数合理"
Write-Host "   4. 第三方CDN地址可以忽略"

